package com.doudizu.shoesonline.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.doudizu.shoesonline.common.BaseResponse;
import com.doudizu.shoesonline.common.ResultUtils;
import com.doudizu.shoesonline.model.dto.link.LinkQuery;
import com.doudizu.shoesonline.model.entity.Link;
import com.doudizu.shoesonline.service.LinkService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ln
 */
@RestController
@RequestMapping("/advert")
@Api(tags = "广告模块")
public class AdvertController {
    @Resource
    private LinkService linkService;

    /**
     *  查看所有的广告
     * @return 存储广告的集合
     */
    @GetMapping("/list")
    @ApiOperation("查看所有广告")
    public BaseResponse<List<Link>> list() {
        LambdaQueryWrapper<Link> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Link::getType, 1);
        List<Link> list = linkService.list(lambdaQueryWrapper);
        return ResultUtils.success(list);
    }

    /**
     *  分页查询
     * @return 分页信息
     */
    @GetMapping("/page")
    @ApiOperation("广告分页查询")
    public BaseResponse page(LinkQuery linkQuery) {
        return ResultUtils.success(linkService.getLinks(linkQuery));
    }

    /**
     *  模糊搜索广告
     * @param keyWord  模糊查询的关键字
     * @return 符合条件的数据
     */
    @GetMapping("/select")
    @ApiOperation("搜索广告")
    public BaseResponse<List<Link>> select(String keyWord) {
        if (StringUtils.isBlank(keyWord)) {
            return this.list();
        }
        LambdaQueryWrapper<Link> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Link::getType, 1)
                .like(Link::getLinkName, keyWord)
                .or().like(Link::getUrl, keyWord)
                .or().like(Link::getEndTime, keyWord);
        List<Link> links = linkService.list(lambdaQueryWrapper);
        return ResultUtils.success(links);
    }

    /**
     * 添加广告
     * @param link 广告实体
     * @return 是否查询成功
     */
    @PostMapping("/add")
    @ApiOperation("增加广告")
    public BaseResponse<String> save(@RequestBody Link link) {
        link.setType(1);
        linkService.save(link);
        return ResultUtils.success("增加公司名称、广告收入、广告有效期、外接URL成功");
    }

    /**
     *  删除广告
     * @param ids 广告的id集合
     * @return
     */
    @PostMapping("/delete")
    @ApiOperation("单个或者批量删除广告信息")
    public BaseResponse delete(@RequestParam List<Integer> ids) {
        linkService.removeBatchByIds(ids);
        return ResultUtils.success(null);
    }
}
